import { createRouter, createWebHistory} from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
   {path:"/",redirect:"/home"},
   {path:'/home',name:"home",components:{
    default:()=>import("@/views/Home/HomeView.vue"),
     footer:()=>import("@/components/FooterCom.vue")
    }},
   {path:'/kind',name:"kind",components:{
    default:()=>import("@/views/KindView/KindView.vue"),
    footer:()=>import("@/components/FooterCom.vue")
  }},
   {path:'/cart',name:"cart",components:{
     default:()=>import("@/views/CartView/CartView.vue"),
      footer:()=>import("@/components/FooterCom.vue")
   }},
   {path:'/my',name:"my",components:{
    default:()=>import("@/views/MyView/MyView.vue"),
    footer:()=>import("@/components/FooterCom.vue")
   }},
   {path:"/detail/:proid",name:"detail",component:()=>import("@/views/Detail/DetailView.vue")},
   {path:"/register",name:"register",component:()=>import("@/views/Register/RegisterView.vue"),children:[
    {path:"/register/",redirect:"/register/tel"},
    {path:"tel",component:()=>import("@/views/Register/components/CheckTel.vue")},
    {path:"msg",component:()=>import("@/views/Register/components/SendMsg.vue")},
    {path:"pass",component:()=>import("@/views/Register/components/SetPass.vue")},
   ]},
   {path:"/login",name:"login",component:()=>import("@/views/Register/components/LoginView.vue")},
  ],
})
router.beforeEach((to,from,next)=>{
  const needAuth=["cart","my"]
  if(needAuth.includes(to.name)){
    const token=localStorage.getItem('token');
    if(token){
      next();
    }else{
      next({
        path:'/login',
        query:{redirect:to.fullPath}
      })
    }
  }else{
    next();
  }
})

export default router
